package com.online.teaching.module.teacher.homework.controller;

import com.github.pagehelper.PageInfo;
import com.online.teaching.module.teacher.homework.domain.dto.AnswerCorrentingDTO;
import com.online.teaching.module.teacher.homework.domain.dto.HomeWorkAddDTO;
import com.online.teaching.module.teacher.homework.domain.vo.GetAnswerListVO;
import com.online.teaching.module.teacher.homework.domain.vo.GetHomeWorkListVO;
import com.online.teaching.module.teacher.homework.service.TeacherHomeworkService;
import com.online.teaching.utils.Result;
import com.online.teaching.utils.StatusCode;
import com.online.teaching.utils.TokenDecode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @ClassName : StudentBlogController  //类名
 * @Description :   //描述
 * @Author : YG  //作者
 * @Date: 2020-11-04 13:20  //时间
 */
@Api(tags ="老师管理作业模块")
@RestController
public class TeacherHomeworkController {

    @Autowired
    private TeacherHomeworkService teacherHomeworkService;

    @Autowired
    private TokenDecode tokenDecode;

    @ApiOperation(value = "老师发布作业", notes = "老师发布作业")
    @PostMapping("teacher/homework/addHomework")
    public Result addHomework(@Valid @RequestBody HomeWorkAddDTO homeWorkAddDTO)
    {
        Integer id = Integer.valueOf(tokenDecode.getUserInfo().get("id"));
        int i = teacherHomeworkService.addHomework(homeWorkAddDTO, id);
        return i==1?new Result(true, StatusCode.OK,"发布作业成功"):new Result(true,StatusCode.ERROR,"发布作业失败");
    }

    @ApiOperation(value = "老师撤销作业", notes = "老师撤销作业")
    @GetMapping("teacher/homework/deleteHomework")
    public Result deleteHomework(@ApiParam("作业id") @RequestParam Integer homeworkId)
    {
        Integer id = Integer.valueOf(tokenDecode.getUserInfo().get("id"));
        int i = teacherHomeworkService.deleteHomework(homeworkId, id);
        return i==1?new Result(true, StatusCode.OK,"发布作业成功"):new Result(true,StatusCode.ERROR,"发布作业失败");
    }

    @ApiOperation(value = "获取已发布作业列表", notes = "获取已发布作业列表")
    @GetMapping("teacher/homework/addHomework")
    public Result<PageInfo<GetHomeWorkListVO>> getHomeWorkList(@ApiParam("开课id") @RequestParam("teachesId")Integer teachesId,
                                  @ApiParam("第几页")@RequestParam("page") Integer page,
                                  @ApiParam("数量")@RequestParam("size") Integer size)
    {
        Integer id = Integer.valueOf(tokenDecode.getUserInfo().get("id"));
        PageInfo<GetHomeWorkListVO> homeWorkList = teacherHomeworkService.getHomeWorkList(teachesId, id, page, size);
        return new Result(true,StatusCode.OK,"查询成功",homeWorkList);
    }

    @ApiOperation(value = "点击查看作业作答列表", notes = "点击查看作业作答列表")
    @GetMapping("teacher/homework/getAnswerList")
    public Result<PageInfo<GetAnswerListVO>> getAnswerList(@ApiParam("homework_id") @RequestParam("id")Integer id,
                                @ApiParam("第几页")@RequestParam("page") Integer page,
                                @ApiParam("数量")@RequestParam("size") Integer size)
    {
        PageInfo<GetAnswerListVO> answerList = teacherHomeworkService.getAnswerList(id, page, size);
        return new Result(true,StatusCode.OK,"查询成功",answerList);
    }

    @ApiOperation(value = "批改作业", notes = "批改作业")
    @GetMapping("teacher/homework/correctingAnswer")
    public Result correctingAnswer(@Valid @RequestBody AnswerCorrentingDTO answerCorrentingDTO)
    {
        Integer id = Integer.valueOf(tokenDecode.getUserInfo().get("id"));
        int i = teacherHomeworkService.correctingAnswer(answerCorrentingDTO, id);
        return i==1?new Result(true, StatusCode.OK,"批改作业成功"):new Result(true,StatusCode.ERROR,"批改作业失败");
    }
}
